<template>
  <el-dialog
    title="产品BOM物料选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="false"
    width="60%"
    center
    :close-on-click-modal ="false"
  >
    <!-- <el-form
      :model="queryParams"
      ref="queryParams"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="物料名称" prop="sonname">
        <el-input
          v-model="queryParams.sonname"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <div class="per">

    
    <el-table
      v-loading="loading"
      :data="bomList"
      @current-change="handleCurrent"
      @row-dblclick="handleRowDbClick"
      @selection-change="handleSelectionChange"
      max-height="620"
      row-key="bomItemCode"
      :row-class-name="tableRowClassName"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column width="50" align="center">-->
      <!--        <template v-slot="scope">-->
      <!--          <el-radio v-model="selectedItemId" :label="scope.row.bomItemId" @change="handleRowChange(scope.row)">-->
      <!--            {{ "" }}-->
      <!--          </el-radio>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="物料编码" align="center" prop="itemCode" />
      <el-table-column label="物料名称" align="center" prop="sonname" />
      <el-table-column label="规格" align="center" prop="specification" />

      <el-table-column label="用量:分子" align="center" show-overflow-tooltip prop="jdyNumerator">
        
      </el-table-column>

      <el-table-column label="用量:分母" width="90px" align="center" prop="jdyDenominator">
        <template slot-scope="scope">
          <span>
            {{
            scope.row.jdyDenominator ? scope.row.jdyDenominator : 1
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="measureName" />
      <!-- <el-table-column label="使用比例" align="center" prop="quantity"/> -->
      <!-- <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />-->
    </el-table>
  </div>
    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />-->
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listBom, routeBOMList } from "@/api/mes/md/bom";
import {
  addRouteproductbom,
  updateRouteproductbom
} from "@/api/mes/pro/routeproductbom";

export default {
  name: "ItemBomSelectSingle",
  props: {
    itemId: undefined,
    routeId: undefined,
    processId: undefined,
    processCode: undefined,
    processName: undefined
  },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      selectedItemId: undefined,
      selectedRows: undefined,
      selectedData: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品BOM关系表格数据
      bomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        productId: this.itemId,
        pageNum: 1,
        pageSize: 999,
        // itemId: this.itemId,
        jdyItemId: this.jdyItemId,
        bomItemId: null,
        bomItemCode: null,
        bomItemName: null,
        bomItemSpec: null,
        unitOfMeasure: null,
        quantity: null,
        enableFlag: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      },
      //新增确定后保存的id数组
      Ids: [],
      //回显的list
      list: []
    };
  },
  created() {
    // this.getList();
  },
  watch: {
    itemId: function(val) {
      this.itemId = val;
      this.queryParams.productId = val;
      if (!!val) {
        this.getList();
      }
      
    }
  },
  methods: {
    init(row,repetitive) {
      this.getList();
      console.log(row, "回显的row");
      this.list = row || [];
      this.repetitive = repetitive
    },
    /** 查询产品BOM关系列表 */
    getList() {
      //debugger;
      this.loading = true;
      routeBOMList(this.queryParams).then(response => {
        console.info("response.rows====bom====>", response);
        this.bomList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    //搜索
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.info("selection===", selection);
      this.selectedData = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.ids = selection.map(item => item.itemCode);
      let list = this.repetitive.map(item => item.itemCode);
      let repetitive=this.compare(list,this.ids)
      console.log(repetitive,'之前提交了 现在又选了的');
      if(repetitive.length!=0){
        this.$modal.msgError("产品物料编码为"+repetitive+"已被选择")
      }

    },

    //确定选中
    confirmSelect() {
      if (this.ids == null || this.ids == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!"
        });
        return;
      } else {
        this.showFlag = false;
        this.selectedData.forEach(obj => {
          obj.processId = this.processId;
          (obj.processCode = this.processCode),
            (obj.processName = this.processName),
            (obj.routeId = this.routeId);
          obj.productId = this.itemId;
        });

        // 转换字段名称
        let backEndData = this.selectedData.map(obj => {
          return {
            processCode: obj.processCode,
            processName: obj.processName,
            processId: obj.processId,
            routeId: obj.routeId,
            productId: obj.productId,
            itemName: obj.sonname,
            itemId: obj.bomItemId,
            itemCode: obj.itemCode,
            specification: obj.specification,
            jdyNumerator: obj.jdyNumerator,
            jdyDenominator: obj.jdyDenominator,
            unitOfMeasure: obj.measureName
            // unitOfMeasure:obj.measureName
          };
        });
        console.info("backEndData2222", backEndData);

        // let ids =selection.map(item => item.itemCode);
        let list = this.repetitive.map(item => item.itemCode);
         console.log(this.ids,'这次的');
         console.log(list,'之前的');
        let repetitive=this.compare(list,this.ids)
        console.log(repetitive,'之前提交了 现在又选了的');
        if(repetitive.length!=0){
          this.$modal.msgError("产品物料编码为"+repetitive+"已被选择")
          return 
        }

        //取出存储的ids
        // let Ids = JSON.parse(localStorage.getItem("Ids"));
        // console.log(Ids,'取出的Ids');
        // if(Ids!=null){
        //   //Ids2是这次选择的
        //   let Ids2 = backEndData.map(item=>{
        //     return item.itemCode
        //   })
        //   //调用函数 判断两个数组中相同的元素
        //   let arr=this.compare(Ids2,Ids)
        //   console.log(arr,'找出相同的元素'); //  找出相同的元素
        //   if(arr.length!=0){
        //     this.$modal.msgError("产品物料编码为"+arr+"已被选择")
        //     return
        //   }else{
        //     this.Ids=[]
        //     //如果存储的有数组 跟Ids合并
        //     //Ids2.concat(Ids)
        //     console.log(Ids,'不是第一次时没有相同的时存储的Ids');
        //     console.log(Ids2,'不是第一次时没有相同的时存储的Ids2');
        //     this.Ids = Ids2.concat(Ids);
        //     console.log(this.Ids,'不是第一次时没有相同的时存储的this.Ids');
        //     localStorage.setItem("Ids", JSON.stringify(this.Ids));
        //     this.$modal.msgSuccess("新增成功");
        //   }
        // }else{
        //第一次新增
        backEndData.forEach(item => {
          this.Ids.push(item.itemCode);
        });
        // console.log(this.Ids,'存储的IDs');
        //localStorage.setItem("Ids", JSON.stringify(this.Ids));
        console.log(backEndData, "选择好的数据传给父组件");
        //return;
        // this.$modal.msgSuccess("新增成功");
        // }

        this.open = false;
        this.getList();

        this.$emit("backEndData", backEndData);
        this.ids = null;
        this.selectedData = [];
      }
    },
    compare(arr1, arr2) {
      return arr1.filter(v => {
        return arr2.includes(v);
      });
    },
    //改变颜色
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, "row");
      // console.log(this.list, "this.list");
      let bool = this.list.filter(_ => _.itemCode == row.itemCode);
      if (bool && bool.length > 0) {
        return "success-row";
      }
      return "";
    },
    
  }
};
</script>
<style scoped>
.per ::v-deep .el-table .warning-row {
  background: oldlace;
}
.per ::v-deep .el-table .success-row {
  background: rgba(107, 178, 241, 0.722);
}
.per ::v-deep .el-table tbody tr:hover > td {
background-color: unset !important
}
</style>
